<template>
  <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <a class="navbar-brand" href="#">者也专栏</a>
    <ul v-if="!user.isLogin" class="list-inline mb-0">
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2">登陆</router-link>
      </li>
      <li class="list-inline-item">
        <router-link to="/signup" class="btn btn-outline-light my-2">注册</router-link>
      </li>
    </ul>
    <ul v-else class="list-inline mb-0">
      <li class="list-inline-item">
        <drop-down :title="userName">
          <drop-item>
            <router-link to="/create">
              <a href="#" class="dropdown-item">新建文章</a>
            </router-link>
          </drop-item>
          <drop-item disabled>
            <router-link to="/edit">
              <a href="#" class="dropdown-item">编辑资料</a>
            </router-link>
          </drop-item>
          <drop-item><a @click="logOut" href="#" class="dropdown-item">退出登录</a></drop-item>
        </drop-down>
      </li>
    </ul>
  </nav>
</template>

<script lang="ts">

import { computed, defineComponent, PropType, ref } from 'vue'
import DropDown from '@/components/DropDown.vue'
import DropItem from '@/components/DropItem.vue'
import { UserProps } from '@/store'
import { useStore } from 'vuex'

export default defineComponent({
  name: 'GlobalHeader',
  components: {
    DropItem,
    DropDown
  },
  props: {
    user: {
      type: Object as PropType<UserProps>,
      required: true
    }
  },
  setup () {
    const store = useStore()
    const userName = ref(store.state.user.nickName)
    const logOut = () => {
      store.dispatch('logout')
    }
    return {
      userName,
      logOut
    }
  }
})
</script>

<style scoped lang="less">

</style>
